package com.jb.repair.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by gaobin on 2015/11/24.
 */
public class DBHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "modify_db";
    public static final String SI_RESULT_TABLE_NAME = "tb_si_task_result";
    public static final String ABNORMAL_TABLE_NAME = "TB_EQ_ABNORMAL";
    public static final String DEFECT_TABLE_NAME = "TB_EQ_DEFECT";
    public static final String FILE_TABLE_NAME = "TB_FILE_LB";
    public static final String CT_TASK_TABLE_NAME = "TB_CT_TASK";
    public static final String CT_TASK_MODEL_NAME = "TB_CT_TASK_MODEL";

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + SI_RESULT_TABLE_NAME + " (" +
                "GUID                VARCHAR( 42 )     PRIMARY KEY," +
                "task_id             VARCHAR( 42 )," +
                "task                VARCHAR( 512 )," +
                "plan_task_detail_id VARCHAR( 42 )," +
                "start_date          DATETIME," +
                "is_must_today       VARCHAR(42)," +
                "desc_result         VARCHAR(42)," +
                "num_result          DECIMAL(32,4)," +
                "is_abnormal         VARCHAR(42)," +
                "is_defect           VARCHAR(42)," +
                "abnormal_desc       VARCHAR(512)," +
                "defect_desc         VARCHAR(512)," +
                "defect_level_id     VARCHAR(42)," +
                "defect_type_guid    VARCHAR(42)," +
                "defect_describe     VARCHAR(120)," +
                "attachment          VARCHAR(512)," +
                "si_person_id        VARCHAR(42)," +
                "si_person           VARCHAR(256)," +
                "si_time             DATETIME," +
                "is_forgot           VARCHAR( 42 )," +
                "note                VARCHAR(512)," +
                "power_plant_id9     VARCHAR(42)," +
                "creat_time          DATETIME," +
                "update_time         DATETIME," +
                "is_repeat           VARCHAR( 42 )," +

                // 以下为新增加部分，计划任务详情字段 2016/3/29 孙奉刚

                "plan_id             VARCHAR(42)," +
                "plan                VARCHAR(512)," +
                "dev_id              VARCHAR(42)," +
                "dev_name            VARCHAR(512)," +
                "place_id            VARCHAR(42)," +
                "place               VARCHAR(512)," +
                "si_standard         VARCHAR(42)," +
                "position            VARCHAR(512)," +
                "content             VARCHAR(512)," +
                "standard            VARCHAR(512)," +
                "cycle               VARCHAR(42)," +
                "method              VARCHAR(42)," +
                "tools               VARCHAR(42)," +
                "standard_max        DECIMAL(32,4)," +
                "standard_min        DECIMAL(32,4)," +
                "standard_normal     DECIMAL(32,4)," +
                "unit                VARCHAR(42)," +
                "del_mark            VARCHAR(42)" +

                ")");

        db.execSQL("CREATE TABLE TB_EQ_DEFECT (" +
                "guid                  VARCHAR( 42 )," +
                "layer_code            VARCHAR( 20 )," +
                "eq_id                 VARCHAR( 42 )," +
                "attacheq_id           VARCHAR( 42 )," +
                "source_id             VARCHAR( 42 )," +
                "found_time            DATETIME," +
                "foundman_id           VARCHAR( 42 )," +
                "level_id              VARCHAR( 42 )," +
                "type_id               VARCHAR( 42 )," +
                "defect_status         VARCHAR( 20 )," +
                "defect_describe         VARCHAR( 20 )," +
                "solved_time_editable  DATETIME," +
                "solvedman_id_editable VARCHAR( 42 )," +
                "solved_result         VARCHAR( 256 )," +
                "attachment            VARCHAR( 256 )," +
                "attachment_path       VARCHAR( 256 )," +
                "inspect_id            VARCHAR( 42 )," +
                "patrol_id             VARCHAR( 42 )," +
                "note                  VARCHAR( 512 )," +
                "power_plant_id        VARCHAR( 42 )," +
                "creat_user_id         VARCHAR( 42 )," +
                "creat_time            DATETIME," +
                "update_user_id        VARCHAR( 42 )," +
                "update_time           DATETIME" +
                ")");
        db.execSQL("CREATE TABLE TB_EQ_ABNORMAL (" +
                "guid                  VARCHAR( 42 )," +
                "layer_code            VARCHAR( 20 )," +
                "eq_id                 VARCHAR( 42 )," +
                "attacheq_id           VARCHAR( 42 )," +
                "source_id             VARCHAR( 42 )," +
                "found_time            DATETIME," +
                "foundman_id           VARCHAR( 42 )," +
                "abn_describe          VARCHAR( 120 )," +
                "abn_status            VARCHAR( 20 )," +
                "solved_time_editable  DATETIME," +
                "solvedman_id_editable VARCHAR( 42 )," +
                "dealtype_id           VARCHAR( 42 )," +
                "attachment            VARCHAR( 256 )," +
                "attachment_path       VARCHAR( 256 )," +
                "inspect_id            VARCHAR( 42 )," +
                "patrol_id             VARCHAR( 42 )," +
                "note                  VARCHAR( 512 )," +
                "power_plant_id        VARCHAR( 42 )," +
                "creat_user_id         VARCHAR( 42 )," +
                "creat_time            DATETIME," +
                "update_user_id        VARCHAR( 42 )," +
                "update_time           DATETIME " +
                ")");
        db.execSQL("CREATE TABLE TB_FILE_LB (" +
                "GUID          VARCHAR( 42 )      PRIMARY KEY," +
                "OBJ_ID        VARCHAR( 42 )," +
                "WJMC          VARCHAR( 200 )," +
                "LX            VARCHAR( 100 )," +
                "DX            DECIMAL( 65, 30 )," +
                "SCSJ          DATETIME," +
                "CCID          VARCHAR( 42 )," +
                "WCMC          VARCHAR( 42 )," +
                "WJMCBM        VARCHAR( 42 )," +
                "WJYLBM        VARCHAR( 42 )," +
                "table_name    VARCHAR( 42 )," +
                "column_name   VARCHAR( 42 )," +
                "line_id       VARCHAR( 42 )," +
                "ZHZT          CHAR( 2 )," +
                "FILE_KEY_WORD TEXT," +
                "DW            VARCHAR( 42 )," +
                "TIMEPOINT     DATETIME" +
                ")"
        );
        // 定期工作表
        db.execSQL("CREATE TABLE " + CT_TASK_TABLE_NAME + " (" +
                "GUID          VARCHAR( 42 )      PRIMARY KEY," +
                "plan_id       VARCHAR( 42 )," +
                "task_name     VARCHAR( 20 )," +
                "gznr   VARCHAR( 256 )," +
                "task_status   VARCHAR( 20 )," +
                "push_status   VARCHAR( 20 )," +
                "ct_person_id  VARCHAR( 42 )," +
                "ct_person_name  VARCHAR( 42 )," +
                "should_finish_time       DATETIME," +
                "remind_time        DATETIME," +
                "finished_time    DATETIME," +
                "registered_time  DATETIME," +
                "delay_time       DATETIME," +
                "delay_result     TEXT," +
                "cancel_result  TEXT," +
                "completion     TEXT," +
                "attachment     VARCHAR( 256 )," +
                "attachment_path     VARCHAR( 256 )," +
                "note     VARCHAR( 512 )," +
                "is_enable     VARCHAR( 42 )," +
                "creat_person_id     VARCHAR( 42 )," +
                "power_plant_id     VARCHAR( 42 )," +
                "creat_time     DATETIME," +
                "update_time    DATETIME" +
                ")"
        );


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public boolean deleteDatabase(Context context) {
        return context.deleteDatabase(DATABASE_NAME);
    }
}
